from tortoise import BaseDBAsyncClient


async def upgrade(db: BaseDBAsyncClient) -> str:
    return """
        DROP INDEX IF EXISTS "idx_device_status_b5b573";
        DROP INDEX IF EXISTS "idx_device_is_acti_3f5e22";
        DROP INDEX IF EXISTS "idx_device_last_ch_0597df";
        DROP INDEX IF EXISTS "idx_device_brand_i_c29865";
        DROP INDEX IF EXISTS "idx_device_area_id_903a1d";
        DROP INDEX IF EXISTS "idx_device_device__9afeac";
        ALTER TABLE "devices" RENAME TO "device";
        ALTER TABLE "device" ADD "extra_info" JSONB;
        ALTER TABLE "device" ADD "rack_location" VARCHAR(200);
        ALTER TABLE "device" DROP COLUMN "last_check_time";
        ALTER TABLE "device" DROP COLUMN "version";
        ALTER TABLE "device" DROP COLUMN "is_active";
        ALTER TABLE "device" ALTER COLUMN "status" SET DEFAULT 'offline';
        COMMENT ON COLUMN "device"."serial_number" IS '设备序列号';
        CREATE INDEX IF NOT EXISTS "idx_device_serial__b98e5e" ON "device" ("serial_number");"""


async def downgrade(db: BaseDBAsyncClient) -> str:
    return """
        DROP INDEX IF EXISTS "idx_device_serial__b98e5e";
        ALTER TABLE "device" RENAME TO "devices";
        ALTER TABLE "device" ADD "last_check_time" TIMESTAMPTZ;
        ALTER TABLE "device" ADD "version" VARCHAR(100);
        ALTER TABLE "device" ADD "is_active" BOOL NOT NULL DEFAULT True;
        ALTER TABLE "device" DROP COLUMN "extra_info";
        ALTER TABLE "device" DROP COLUMN "rack_location";
        ALTER TABLE "device" ALTER COLUMN "status" SET DEFAULT 'unknown';
        COMMENT ON COLUMN "device"."serial_number" IS '序列号';
        CREATE INDEX IF NOT EXISTS "idx_device_device__9afeac" ON "device" ("device_group_id", "status");
        CREATE INDEX IF NOT EXISTS "idx_device_area_id_903a1d" ON "device" ("area_id", "status");
        CREATE INDEX IF NOT EXISTS "idx_device_brand_i_c29865" ON "device" ("brand_id", "status");
        CREATE INDEX IF NOT EXISTS "idx_device_last_ch_0597df" ON "device" ("last_check_time");
        CREATE INDEX IF NOT EXISTS "idx_device_is_acti_3f5e22" ON "device" ("is_active");
        CREATE INDEX IF NOT EXISTS "idx_device_status_b5b573" ON "device" ("status");"""
